package cn.edu.scau.cmi.mazhuanglong.comprehension.Client;

import java.util.List;
import java.util.Scanner;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.mazhuanglong.comprehension.util.ApplicationContextUtil;
import cn.edu.scau.cmi.mazhuanglong.comprehension.Dao.StudentDAO;
import cn.edu.scau.cmi.mazhuanglong.comprehension.Dao.TeacherDAO;
import cn.edu.scau.cmi.mazhuanglong.comprehension.domain.Student;
import cn.edu.scau.cmi.mazhuanglong.comprehension.domain.Teacher;
import cn.edu.scau.cmi.mazhuanglong.comprehension.util.ScauCmiHibernateSessionFactoryUtil;

public class SpringClient {
	static Scanner input = new Scanner(System.in);

	public static void main(String[] args) {

		String str = "1、添加学生" + "\n" + "2、修改学生" + "\n" + "3、删除学生" + "\n" + "4、添加教师" + "\n" + "5、修改教师" + "\n" + "6、删除教师"
				+ "\n" + "7、学生选择指导老师" + "\n" + "8、展示学生数据表";
		System.out.println(str);
		// int in = input.nextInt();
		while (true) {
			int in = input.nextInt();
			switch (in) {
			case 1: {
				System.out.println("输入姓名");
				String stuname = input.next();

				Student student = new Student();
				student.setName(stuname);

				StudentDAO instance = getStudentDAO();
				instance.save(student);
				break;

			}
			case 2: {
				System.out.println("输入你要修改的学生学号");
				long id = input.nextInt();

				System.out.println("输入你要修改后的学生姓名");
				String name = input.next();

				Student student = new Student();
				student.setId(id);
				student.setName(name);

				StudentDAO instance = getStudentDAO();
				instance.update(student);

				break;
			}
			case 3: {
				System.out.println("输入你要删除的学生学号");
				long id = input.nextInt();
				StudentDAO instance = getStudentDAO();

				instance.delete(instance.findById(id));

				break;
			}
			case 4: {
				System.out.println("输入姓名");
				String stuname = input.next();

				Teacher teacher = new Teacher();
				teacher.setName(stuname);

				TeacherDAO instance = getTeacherDAO();
				instance.save(teacher);
				break;
			}
			case 5: {
				System.out.println("输入你要修改的老师学号");
				long id = input.nextInt();

				System.out.println("输入你要修改后的老师姓名");
				String name = input.next();

				Teacher teacher = new Teacher();
				teacher.setId(id);
				teacher.setName(name);

				TeacherDAO instance = getTeacherDAO();
				instance.update(teacher);
				break;
			}
			case 6: {
				System.out.println("输入你要删除的教师学号");
				long id = input.nextInt();
				TeacherDAO instance = getTeacherDAO();

				instance.delete(instance.findById(id));

				break;
			}
			case 7: {
				System.out.println("选择学生id");
				long studentid = input.nextLong();
				StudentDAO stuinstance = getStudentDAO();
				stuinstance.findById(studentid);

				System.out.println("选择你的老师id");
				long teacherid = input.nextLong();
				TeacherDAO teainstance = getTeacherDAO();
				teainstance.findById(teacherid);

				Student student = new Student();
				student.setId(studentid);
				student.setName(stuinstance.findById(studentid).getName());
				student.setTeacher(teainstance.findById(teacherid));
				stuinstance.update(student);

				break;
			}
			case 8: {
				StudentDAO stu = new StudentDAO();
				List<Student> studentlist = stu.findAll();
				for (Student s : studentlist)

					System.out.println(s.getId() + "\t" + s.getName() + "\t");
				break;
			}
			default:
				System.out.print("运行结束");
				break;
			}
		}

	}

	private static StudentDAO getStudentDAO() {
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		return (StudentDAO) ac.getBean("studentDAO");
	}

	private static TeacherDAO getTeacherDAO() {
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		return (TeacherDAO) ac.getBean("teacherDAO");
	}
}
